// pages/address/address.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_no_data_tip:true,
    openid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'openId',
      success(res) {
        that.setData({
          openid: res.data
        })
        wx.request({
          url: app.globalData.api + '&r=member.address.get_list',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: {
            page: 1,
            openid: res.data
          },
          success: function (res) {
            if (res.data.list.length == 0) {
              that.setData({
                show_no_data_tip: true,
                address_list: res.data.list
              });
              return
            } else {
              that.setData({
                show_no_data_tip: false,
                address_list: res.data.list
              });
            }
          },
          error: function () {
            console.log('请求错误');
          }
        })
      },
      fail() {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  //设置地址
  setAddress:function(url,data){
    var that = this;
    wx.request({
      url: app.globalData.api + url,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data:data,
      success: function (res) {
        that.onLoad();
      },
      error: function () {
        console.log('请求错误');
      }
    })
  },
  // 设置默认地址
  setDefaultAddress:function(e){
    var id = e.currentTarget.dataset.id;
    var data = {id:id,openid:this.data.openid};
    var url = '&r=member.address.set_default';
    this.setAddress(url,data);
  },
  // 删除地址
  deleteAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var data = { id: id, openid: this.data.openid };
    var url = '&r=member.address.delete';
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗？',
      success(res) {
        if (res.confirm) {
          that.setAddress(url, data);
        } else if (res.cancel) {
          return
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'openId',
      success(res) {
        that.setData({
          openid: res.data
        })
        wx.request({
          url: app.globalData.api + '&r=member.address.get_list',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          data: {
            page: 1,
            openid: res.data
          },
          success: function (res) {
            if (res.data.list.length == 0) {
              that.setData({
                show_no_data_tip: true,
                address_list: res.data.list
              });
              return
            } else {
              that.setData({
                show_no_data_tip: false,
                address_list: res.data.list
              });
            }
          },
          error: function () {
            console.log('请求错误');
          }
        })
      },
      fail() {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})